package com.uls.quartz.task;

import com.uls.quartz.domain.SysJob;
import com.uls.quartz.service.ISysJobService;
import com.uls.zsw.domain.*;
import com.uls.zsw.service.*;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.uls.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    @Autowired
    private IElectricityUsageInfoService electricityUsageInfoService;
    @Autowired
    private IElectricityManageService electricityManageService;
    @Autowired
    private IMeterInfoService meterInfoService;
    @Autowired
    private IElectricityBillingService electricityBillingService;
    @Autowired
    private ISysJobService jobService;
    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    @Transactional
    public void electricitycul(){
        //如果用电信息表为空则停止定时任务
        ElectricityUsageInfo electricityUsageInfofind=new ElectricityUsageInfo();
        if(electricityUsageInfoService.selectElectricityUsageInfoList(electricityUsageInfofind).size()==0){
            SysJob sysJob=jobService.findJobByJobName("用电定时任务");
            if(sysJob.getStatus().equals("0")){
                sysJob.setStatus("1");
                try {
                    jobService.changeStatus(sysJob);
                    return;
                } catch (SchedulerException e) {
                    e.printStackTrace();
                }
            }
        }
        //任务代码
        ElectricityManage electricityManage=new ElectricityManage();
        electricityManage.setElectricityStatus("1");
        List<ElectricityManage> Elist=electricityManageService.selectElectricityManageList(electricityManage);//找到电源为开启状态的宿舍
        for(ElectricityManage electricityManage1:Elist){
            Long Did=electricityManage1.getDormitoryId();
            ElectricityUsageInfo electricityUsageInfo=electricityUsageInfoService.selectElectricityUsageInfoByDormitoryId(Did);
            if(electricityUsageInfo!=null) //对宿舍的电表进行用电模拟
            {
                Long Mid=electricityUsageInfo.getMeterId();
                MeterInfo meterInfo=meterInfoService.selectMeterInfoByMeterId(Mid);
                BigDecimal Power=meterInfo.getMeterPower(); //获取电表平均功率
                Random random = new Random();
                int usedhour=random.nextInt(12)+1; //生成0~12的随机数模拟用电时间
                BigDecimal used=BigDecimal.valueOf(usedhour);
                BigDecimal corrent=electricityUsageInfo.getEndReading();
                electricityUsageInfo.setEndReading(corrent.add(Power.multiply(used)));
                electricityUsageInfo.setEndDate(electricityUsageInfo.getEndDate()+1);
                if(electricityUsageInfo.getEndDate()%30==0&&electricityUsageInfo.getEndDate()!=0){ //按30天/月生成账单
                    ElectricityBilling electricityBilling=new ElectricityBilling();
                    electricityBilling.setElectricityUsageId(electricityUsageInfo.getElectricityUsageId());
                    Date starttime=electricityUsageInfo.getStartDate();
                    int year=starttime.getYear()+1900;
                    int month=starttime.getMonth();
                    if(month+electricityUsageInfo.getEndDate()%30>12){
                        year=year+1;
                        month=1;
                    }else {
                        month=month+electricityUsageInfo.getEndDate()/30;
                    }
                    electricityBilling.setBillingMonth(year+"年"+month+"月");
                    BigDecimal totolused=electricityUsageInfo.getEndReading()
                            .subtract(electricityUsageInfo.getStartReading())
                            .subtract(electricityUsageInfo.getElectricityUsed());
                    electricityBilling.setTotalCost(totolused.multiply(BigDecimal.valueOf(1.2)));
                    electricityBillingService.insertElectricityBilling(electricityBilling);
                    electricityUsageInfo.setElectricityUsed(totolused);
                }
                electricityUsageInfoService.updateElectricityUsageInfo(electricityUsageInfo);
            }
        }
    }
}
